Skip to content

SliceCheckableProxyModel

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

⋔ Inheritance diagram

graph TD
  1789169524["itemmodels.SliceCheckableProxyModel"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4214724289 --> 1789169524
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model to make a model checkable.

Example

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table.proxifier[::2, 2:].modify(xyz)
table.show()
# or
indexer = (slice(None, None, 2), slice(2, None))
proxy = itemmodels.SliceCheckableProxyModel(indexer=indexer)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt/itemmodels/proxies/slicecheckableproxymodel.py
class SliceCheckableProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to make a model checkable.

    ### Example

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table.proxifier[::2, 2:].modify(xyz)
    table.show()
    # or
    indexer = (slice(None, None, 2), slice(2, None))
    proxy = itemmodels.SliceCheckableProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "checkable"
    checkstate_changed = core.Signal(core.ModelIndex, bool)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._checked: set[tuple[int, int]] = set()

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        if not index.isValid():
            return super().flags(index)
        if self.indexer_contains(index):
            return super().flags(index) | constants.IS_CHECKABLE
        return super().flags(index)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if role == constants.CHECKSTATE_ROLE and self.indexer_contains(index):
            key = self.get_index_key(index, include_column=True)
            return self.to_checkstate(key in self._checked)
        return super().data(index, role)

    def setData(
        self,
        index: core.ModelIndex,
        value: Any,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        if role == constants.CHECKSTATE_ROLE and self.indexer_contains(index):
            key = self.get_index_key(index, include_column=True)
            checked = self.from_checkstate(value)
            if checked == (key in self._checked):
                return True
            if checked:
                self._checked.add(key)
            else:
                self._checked.discard(key)
            self.dataChanged.emit(index, index, [constants.CHECKSTATE_ROLE])
            self.checkstate_changed.emit(index, checked)
            return True
        return super().setData(index, value, role)

:fa-brands-github: Show source on GitHub